package com.shenmazong.thread;

/**
 * @program: server-java-demo
 * @description: synchronizedTest
 * @author: 亮子说编程
 * @create: 2020-10-20 15:33
 **/
public class synchronizedTest2 {

    public synchronized void method1() {
        System.out.println("Method 1 start");
        try {
            System.out.println("Method 1 execute");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Method 1 end");
    }

    public synchronized void method2() {
        System.out.println("Method 2 start");
        try {
            System.out.println("Method 2 execute");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Method 2 end");
    }


    public static void main(String[] args) {
        final synchronizedTest2 test = new synchronizedTest2();

        new Thread(test::method1).start();

        new Thread(test::method2).start();
    }
}
